/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cs342.project1.mathNotebook.tests.data;

import static org.junit.Assert.*;

import java.io.*;
import java.util.*;

import org.junit.Before;
import org.junit.Test;

import Project2.cs342.project1.mathNotebook.data.*;
import Project2.cs342.project1.mathNotebook.notebook.NotebookEntry;
import java.math.*;

/**
 *
 * @author Kevin
 */
public class EncryptionAndCompressionTests {
    


    /**
    * Checks to see if compression is successful
    */
    @Test
    public void testCompression(){

        List<NotebookEntry> entries = new ArrayList<NotebookEntry>();
	NotebookEntry entry = new NotebookEntry();
	entry.functionString = "2x+3";
	entries.add(entry);

        NotebookWriter writer = new XMLNotebookRepositoryFactory("test.xml").createWriter();
        writer.writeAll(entries);
        writer.close();
        NotebookZip zip = new NotebookZip();
        Boolean result = zip.compressFile("test.xml");
        assertEquals(true, result);
    }

    /**
    * Checks to see if local encryption and decryption is successful.  Must be tested together because the RSA keys and DES key is forgotten between tests.
    */
    @Test
    public void testLocalEncryptionAndDecryption(){
        Boolean result = false;
        RSAKeyGenerator rsaKeyGenerator = new RSAKeyGenerator();
        rsaKeyGenerator.generateKeys();
        User user = new User();
        user.setRsaPublicKey(rsaKeyGenerator.getPublicKey());
        user.setRsaPrivateKey(rsaKeyGenerator.getPrivateKey());
        user.setDesDefaultKey("password");
        LocalEncryptionWriter encryptedFile = new LocalEncryptionWriter(user, user.getDesDefaultKey());
        Boolean writeResult = encryptedFile.writeEncryptedFile("test.gzip");
        BigInteger encryptedDES = encryptedFile.getEncryptedSecretKey();
        LocalEncryptionReader decryptedFile = new LocalEncryptionReader(user, encryptedDES);
        Boolean readResult = decryptedFile.readEncryptedFile("test.encrypt");
        if(writeResult == true && readResult == true){
            result = true;
        }
        assertEquals(true, result);
    }



    /**
    * Checks to see if database encryption and decryption is successful.  Must be tested together because the RSA keys and DES key is forgotten between tests.
    */
    @Test
    public void testDatabaseEncryptionAndDecryption(){
        Boolean result = false;
        RSAKeyGenerator rsaKeyGenerator = new RSAKeyGenerator();

        rsaKeyGenerator.generateKeys();
        Group group = new Group();
        group.setRsaPublicKey(rsaKeyGenerator.getPublicKey());
        group.setRsaPrivateKey(rsaKeyGenerator.getPrivateKey());
        group.setDesDefaultKey("password");
        DatabaseEncryptionWriter encryptedFile = new DatabaseEncryptionWriter(group, group.getDesDefaultKey());
        Boolean writeResult = encryptedFile.writeEncryptedFile("test.gzip");
        BigInteger encryptedDES = encryptedFile.getEncryptedSecretKey();
        DatabaseEncryptionReader decryptedFile = new DatabaseEncryptionReader(group, encryptedDES);
        Boolean readResult = decryptedFile.readEncryptedFile("test.encrypt");
        if(writeResult == true && readResult == true){
            result = true;
        }
        assertEquals(true, result);
    }


    /**
    * Checks to see if Decompression is successful
    */
    @Test
    public void testDecompression(){
        NotebookZip zip = new NotebookZip();
        Boolean result = zip.decompressFile("test.gzip");
        assertEquals(true, result);
    }

}
